import pathlib
import time
import re
import typing
import os
import yaml
import ctypes
import psutil
import subprocess
import requests
import winsound
import datetime
import rich
from selenium.webdriver.edge.webdriver import WebDriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import *







def On_SelectPage_UploadMainPicture_ByPictureNameStr(driver:WebDriver,NameStr:str):
    """批量上传图片批量上传主图图片

    Args:
        driver (WebDriver): WebDriver 实例
        NameStr (str): 上传的路径字符串.
    """
    # 判断是否处于 [以图发品] 页面
    try:
        element = WebDriverWait(driver=driver,timeout=5).until(expected_conditions.presence_of_element_located((By.XPATH,f"//*[contains(@class,'active')]//*[text()='以图发品']")))
    except TimeoutException:
        print(f"提示 : 没有找到选择 [以图发品] 的页面!")
        return 
    
    try:
        element = WebDriverWait(driver=driver,timeout=5).until(expected_conditions.presence_of_element_located((By.XPATH,f"//input[@type='file'][contains(@accept,'image')][@multiple]")))
        element.send_keys(NameStr)
    except TimeoutException:
        print(f"提示 : 没有在 [选择类目页面] 找到 [input] 上传图片控件!")





def On_SelectPage_UploadMainPicture_ByPictureNameList(driver:WebDriver,picture_name_list:list):
    """批量上传主图图片

    Args:
        driver (WebDriver): WebDriver 实例
        picture_name_list (list): 上传的图片路径列表.
    """
    
    # 1.将图片列表转换为 Selenium 可以使用的格式
    picture_list_str = ""
    for index, picture_name in enumerate(picture_name_list):
        picture_path = str(pathlib.Path(picture_name).absolute().resolve())
        if index > 0:  # 不是第一个元素时才添加换行符
            picture_list_str += "\n"
        picture_list_str += picture_path
    print(picture_list_str)

    # 判断是否处于 [以图发品] 页面
    try:
        element = WebDriverWait(driver=driver,timeout=5).until(expected_conditions.presence_of_element_located((By.XPATH,f"//*[contains(@class,'active')]//*[text()='以图发品']")))
    except TimeoutException:
        print(f"提示 : 没有找到选择 [以图发品] 的页面!")
        return 
    
    try:
        element = WebDriverWait(driver=driver,timeout=5).until(expected_conditions.presence_of_element_located((By.XPATH,f"//input[@type='file'][contains(@accept,'image')][@multiple]")))
        element.send_keys(picture_list_str)
    except TimeoutException:
        print(f"提示 : 没有在 [选择类目页面] 找到 [input] 上传图片控件!")





def On_SelectPage_UploadMainPicture_ByDirectory(driver:WebDriver,picture_directory_path:str):
    """批量上传主图图片

    Args:
        driver (WebDriver): WebDriver 实例
        picture_directory_path (str): 需要上传的文件夹路径
    """
    # 1.获取图片列表
    picture_name_list = list()
    for root,dirs,files in os.walk(picture_directory_path):
        for file in files:
            picture_image_path = os.path.join(root,file)
            picture_name_list.append(picture_image_path)
            print(picture_image_path)
    
    
    # 1.将图片列表转换为 Selenium 可以使用的格式
    picture_list_str = ""
    for index, picture_name in enumerate(picture_name_list):
        picture_path = str(pathlib.Path(picture_name).absolute().resolve())
        if index > 0:  # 不是第一个元素时才添加换行符
            picture_list_str += "\n"
        picture_list_str += picture_path
    print(picture_list_str)

    # 判断是否处于 [以图发品] 页面
    try:
        element = WebDriverWait(driver=driver,timeout=5).until(expected_conditions.presence_of_element_located((By.XPATH,f"//*[contains(@class,'active')]//*[text()='以图发品']")))
    except TimeoutException:
        print(f"提示 : 没有找到选择 [以图发品] 的页面!")
        return 
    
    try:
        element = WebDriverWait(driver=driver,timeout=5).until(expected_conditions.presence_of_element_located((By.XPATH,f"//input[@type='file'][contains(@accept,'image')][@multiple]")))
        element.send_keys(picture_list_str)
    except TimeoutException:
        print(f"提示 : 没有在 [选择类目页面] 找到 [input] 上传图片控件!")